package com.study.api;

import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.RandomUtil;
import com.study.api.ApiController.DataDTO.Stu;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono;

import javax.annotation.Resource;
import java.time.LocalDateTime;

/**
 * @author zhuzi
 * 2024/6/19 21:49
 */
@Api(tags = "测试swagger")
@Slf4j
@RestController
@RequestMapping("apa")
public class ApaController {
    @Resource
    RestTemplate restTemplate;

    /**
     * @return {@link Stu }
     */
    @GetMapping("c")
    public Stu apic() {
        String url = "http://127.0.0.1/apa/b";
        Stu forObject = restTemplate.getForObject(url, Stu.class);
        return forObject;
    }

    /**
     * 应用程序接口
     *
     * @return {@link String }
     */
    @GetMapping("a")
    public String api() {
        log.info("A发出请求");
        WebClient webClient = WebClient.create("http://127.0.0.1/apa/b");
        Mono<Stu> stuMono = webClient.get().accept(MediaType.APPLICATION_JSON).retrieve().bodyToMono(Stu.class).doOnError(Exception.class, err -> {  //处理异常
            System.out.println(LocalDateTime.now() + "---发生错误：" + err.getMessage());
        });//.retryBackoff(3, Duration.ofSeconds(5));
        ;
        stuMono.subscribe(s -> {
            log.info("请求返回{}", s);
        }, throwable -> {
            System.out.println(throwable.getClass());
            System.out.println(throwable.getMessage());
            System.out.println("发生错误");
        });
        log.info("A发出请求/end");
        return RandomUtil.randomString(5);
    }

    @GetMapping("b")
    public Object apb() {
        log.info("请求B");
        //System.out.println(1 / 0);
        log.info("测试");
        ThreadUtil.safeSleep(5 * 1000);
        log.info("请求B end");
        Stu stu = new Stu();
        stu.setId(IdUtil.getSnowflakeNextId()).setAge(29).setName("透明");
        return stu;
    }

    @PostMapping("c")
    public Stu formPost(@RequestBody Stu stu) {
        log.info("接收到数据{}", stu);
        Stu stus = new Stu();
        stus.setId(IdUtil.getSnowflakeNextId()).setAge(29).setName("透明");
        return stus;
    }

    @GetMapping("d")
    public String apd() {
        log.info("A发出请求");
        Stu stus = new Stu();

        //https://blog.csdn.net/A_art_xiang/article/details/127025263
        stus.setId(IdUtil.getSnowflakeNextId()).setAge(29).setName("透明-ABC");
        /*WebClient webClient = WebClient.create("http://127.0.0.1:8051/apa/c");
        Mono<Stu> stuMono = webClient.post().accept(MediaType.APPLICATION_JSON)
                .bodyValue(stus)
                .retrieve()
                .bodyToMono(Stu.class)
                .doOnError(Exception.class, err -> {  //处理异常
                    System.out.println(LocalDateTime.now() + "---发生错误：" + err.getMessage());
                });
        ;
        stuMono.subscribe(s -> {
            log.info("请求返回{}", s);
        }, throwable -> {
            System.out.println(throwable.getMessage());
            System.out.println("发生错误");
        });*/
        log.info("A发出请求/end");
        return RandomUtil.randomString(5);
    }

    public void demoO() {

        /*WebClient.create("localhost:8080/employees").get().retrieve().onStatus(HttpStatus::is5xxServerError, clientResponse -> {
            RuntimeException runtimeException = new RuntimeException();
            HttpStatus httpStatus = clientResponse.statusCode();

            return Mono.error(runtimeException);
        }).onStatus(HttpStatus::is4xxClientError, clientResponse -> {
            RuntimeException runtimeException = new RuntimeException();
            HttpStatus httpStatus = clientResponse.statusCode();

            return Mono.error(runtimeException);
        }).onStatus(HttpStatus::isError, clientResponse -> {
            RuntimeException runtimeException = new RuntimeException();
            HttpStatus httpStatus = clientResponse.statusCode();
            return Mono.error(runtimeException);
        }).bodyToFlux(String.class);


        Mono<Stu> stuMono = WebClient.create().get().uri("https://abc.com/account/123").retrieve().bodyToMono(Stu.class).doOnError(Exception.class, err -> {  //处理异常
            System.out.println(LocalDateTime.now() + "---发生错误：" + err.getMessage());
        }).retryBackoff(3, Duration.ofSeconds(5));*/

    }
}
